﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataProvider;
using ERP.Manager.DataEntity;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;
using ERP.Manager.Core;

namespace ERP.Manager.SQLServerProvider
{
    public class WorkBlogRepository : IWorkBlogRepository
    {
        #region 添加个人工作记录信息
        public int addWorkBlog(WorkBloginfo workblog)
        {
            int result = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_WORKBLOG,
              new SqlParameter("@workblogTitle", workblog.workblogTitle),
              new SqlParameter("@workblogContent", workblog.workContent),
              new SqlParameter("@userid", workblog.userid),
              new SqlParameter("@starTime", workblog.starTime),
              new SqlParameter("@note", workblog.note));
            return result;
        }
        #endregion

        #region 根据当前用户查询所有工作记录信息
        public List<WorkBloginfo> getWorkBlogByUserid(string userId, int pagecount)
        {
            List<WorkBloginfo> list = new List<WorkBloginfo>();
            WorkBloginfo workblog = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.GET_WORKBLOG_USERID,
                new SqlParameter("@userid", userId),
                new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    workblog = new WorkBloginfo();
                    workblog.workblogId = int.Parse(read["workblogId"].ToString());
                    workblog.workblogTitle = read["workblogTitle"].ToString();
                    workblog.workContent = read["workblogContent"].ToString();
                    workblog.starTime = DateTime.Parse(read["starTime"].ToString());
                    workblog.userid = read["userid"].ToString();
                    workblog.note = read["note"].ToString();
                    list.Add(workblog);
                }
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 修改个人工作记录
        public int UpdateWorkBlog(WorkBloginfo workblog)
        {
            int result = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_WORKBLOG,
                new SqlParameter("@workblogTitle", workblog.workblogTitle),
                 new SqlParameter("@workblogContent", workblog.workContent),
                 new SqlParameter("@note", workblog.note),
                 new SqlParameter("@workblogid", workblog.workblogId));
            return result;
        }
        #endregion

        #region 根据编号删除个人工作记录的详细信息
        public int DeleteWorkBlog(int workblogId)
        {
            int result = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_WORKBLOG,
              new SqlParameter("@workblogId", workblogId));
            return result;
        }
        #endregion

        #region 根据工作记录编号查询工作的详细信息
        public WorkBloginfo getWorkBlogById(int workblogid)
        {
            WorkBloginfo workblog = null;
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.GETWORKBLOG_BYID, new SqlParameter("@workblogid", workblogid));
            try
            {
                if (read.Read())
                {
                    workblog = new WorkBloginfo();
                    workblog.workblogId = int.Parse(read["workblogId"].ToString());
                    workblog.workblogTitle = read["workblogTitle"].ToString();
                    workblog.workContent = read["workblogContent"].ToString();
                    workblog.starTime = DateTime.Parse(read["starTime"].ToString());
                    workblog.userid = read["userid"].ToString();
                    workblog.note = read["note"].ToString();
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return workblog;
        }
        #endregion

        #region 根据当前用户编号查询所有的工作记录的总数
        public int getallworkblogcountbyuserid(string userid)
        {
            var count = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.GETALL_WORKBLOGCOUNT_BYUSERID, new SqlParameter("@userid", userid));
            return int.Parse(count.ToString());
        }
        #endregion
    }
}
